package com.thon.controller.rest;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Maps;
import com.thon.dao.util.Page;
import com.thon.entity.project.Preferential;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectComment;
import com.thon.entity.project.ProjectCommentView;
import com.thon.entity.user.preferential;
import com.thon.service.project.PreferentialService;
import com.thon.service.project.ProjectCommentService;
import com.thon.service.project.ProjectService;

/**
 * @file ProjectCommentRestController.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月19日 上午9:04:22
 * @description: TODO
 */
@Controller
@RequestMapping(value = "/api")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ProjectCommentRestController {

	@Autowired
	private ProjectService projectService;

	@Autowired
	private PreferentialService preferentialService;
	
	
	@Autowired
	private ProjectCommentService projectCommentService;
	
	/**
	 * @param projectComment
	 * 		content:required = true
	 * 		userId:require = true
	 * 		project.id:require = true
	 * 		superId:require = false 为评论根id
	 * 		commentTo:require = false 为评论给某用户的id
	 * @return 204:评论内容为空，201评论成功
	 * 发起评论，直接评论项目时superId,CommentTo为空，
	 * 评论他人发起的评论时superId为该组评论的根评论的id，commentTo为评论给某用户的id
	 */
	@RequestMapping(value = "/comment",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addComment(@Valid @RequestBody ProjectComment projectComment){
		String msg = new String("");
		Map<String, Object> map = Maps.newHashMap();
		
		if (projectComment.getContent()==null) {
			msg="content_is_null";
		}else{				
			
		
			if(projectComment.getProject()==null)
			{
				
				if(projectComment.getPreferential()==null)
				{
					msg="project or preferential is null";
					map.put("msg", msg);
					return new ResponseEntity(map, HttpStatus.CREATED);
				}
				Preferential preferential = preferentialService.getPreferential(projectComment.getPreferential().getId());
				preferential.setCommentCount(preferential.getCommentCount().intValue()+1);
				preferentialService.updatePreferential(preferential);
			}
			else
			{
				Project project = projectService.getProject(projectComment.getProject().getId());
				project.setCommentCount(project.getCommentCount().intValue()+1);
			projectService.updateProject(project);	}		
			msg="comment_success";
			projectComment.setIsDelete(0);
			projectComment.setIsRead(0);
			projectCommentService.add(projectComment);	
		}
		map.put("msg", msg);
		return new ResponseEntity(map, HttpStatus.CREATED);
	}
	
	/**
	 * @param by 
	 * 			user:获取“用户的评论”
	 * 			project:获取“项目的评论”
	 * 			to:获取“评论我的”
	 * @param id  相对应的id
	 * @param pageSize
	 * @param pageNo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/comment/{by}/list",method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> findComment(
			@PathVariable("by")String by,
			@RequestParam(value = "id",required = true)int id,
			@RequestParam(value = "s",defaultValue = "10")int pageSize,
			@RequestParam(value = "p",defaultValue = "1")int pageNo,Model model){
		
		if(by.equals("user")||by.equals("project")||by.equals("preferential")||by.equals("super")){
			by = by+"_id";
		}else if(by.equals("to")){
			by = "comment_"+by;
		}
		Page<ProjectCommentView> page = projectCommentService.findComment(by, id, pageNo, pageSize);
			
		return new ResponseEntity(page,HttpStatus.OK);
	}
	
	@RequestMapping(value = "/comment/del",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> deleteComment(
							@RequestParam(value = "cid",required = true)int commentId,
							@RequestParam(value = "uid",required = true)int userId){
		Map<String, Object> map = Maps.newHashMap();
		String msg="";
		ProjectComment oldComment = projectCommentService.getComment(commentId);
						
						
		if(oldComment==null)
		{
			msg="null";					
		}	
		else if(oldComment.getIsDelete()==1)
		{
			msg="close";		
		}
		else if (userId == oldComment.getUserId()) {
			oldComment.setIsDelete(1);
			projectCommentService.updateComment(oldComment);
		
			if(oldComment.getProject()==null)
			{
				
				if(oldComment.getPreferential()==null)
				{
					msg="false";
					map.put("msg", msg);
					return new ResponseEntity(map, HttpStatus.CREATED);
				}
				Preferential preferential = preferentialService.getPreferential(oldComment.getPreferential().getId());
				preferential.setCommentCount(preferential.getCommentCount().intValue()-1);
				preferentialService.updatePreferential(preferential);
			}
			else
			{
				Project project =  projectService.getProject(-1,oldComment.getProject().getId());
				project.setCommentCount(project.getCommentCount().intValue()-1);
			projectService.updateProject(project);}
			msg="true";
		}else {
			msg="false";
		}
		map.put("msg", msg);
		return new ResponseEntity(map, HttpStatus.CREATED);
		
	}
	/**
	 * 根据用户Id获取用户的评论列表
	 * @param userId
	 * @param pageNo
	 * @param pageSize
	 * @return Page<ProjectCommentView>
	 */
	@RequestMapping(value="/comment/mycomment",method=RequestMethod.GET)
	public ResponseEntity<HttpEntity> findMyComment(
			@RequestParam(value="uid",required=true) Integer userId,
			@RequestParam(value="method",defaultValue="0") Integer method,			
			@RequestParam(value="p",required=false,defaultValue="1") Integer pageNo,
			@RequestParam(value="s",required=false,defaultValue="5") Integer pageSize){
		Page<ProjectCommentView> myCommentsPage = new Page<ProjectCommentView>();
		projectCommentService.findMyComments(myCommentsPage,userId,method,pageNo,pageSize);
		return new	ResponseEntity(myCommentsPage,HttpStatus.OK);		
	}
}
